package org.example.petadoption.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.example.petadoption.entity.ResultBody;
import org.example.petadoption.entity.User;
import org.example.petadoption.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author PetAdoption
 * @since 2025-10-20
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserService userService;


    //查询表
    @PostMapping("/selectAllUser")
    public ResultBody selectAllUser(){

        List<User> sau = userService.list();
        return ResultBody.success("查询到" + sau.size() + "条数据", sau);

    }

    //用户注册
    @PostMapping("/addUser")
    public ResultBody addUser(@RequestBody User user){
        if (user == null){
            return ResultBody.fail("添加失败");
        }
        if (user.getRole() != null && user.getRole() == 1){
            return ResultBody.fail("不可以添加管理员");
        }
        if (StringUtils.isBlank(user.getUsername())){
            return ResultBody.fail("检查用户名，不能为空");
        }
        if (StringUtils.isBlank(user.getPassword())){
            return ResultBody.fail("检查密码，不能为空");
        }
        if (StringUtils.isBlank(user.getEmail())){
            return ResultBody.fail("检查Emaill，不能为空");
        }
        if (StringUtils.isBlank(user.getPhone())){
            return ResultBody.fail("检查手机号，不能为空");
        }

        boolean result = userService.save(user);
        return result?ResultBody.success():ResultBody.fail();
    }





}
